package engine.newUi
{
	import engine.control.loading.MultLoader;
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.data.ResPath;
	import engine.logic.AlertLogic;
	import engine.logic.LogSendLogic;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.ui.AlertUI;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	
	import newx.com.ResManager;
	import newx.com.config.StaticIni;
	import newx.com.control.NxClip;
	import newx.com.control.button.NxButton;
	import newx.com.control.button.NxButtonState;
	import newx.com.control.window.NxWindow;
	import newx.com.control.window.NxWindowEvent;

	/**
	 *聚宝盆主逻辑 
	 * @author yangyuechao
	 * 
	 */	
	public class TreasureBowlMainLogic
	{
		private static var _self:TreasureBowlMainLogic;
		private var _myDis:TreasureBowlMainUI;
		private var _model:ModelLocator;
		private var _bind:BindManage;
		private var _bindFlag:int = -1;
		/**元宝类型**/
		private static const TYPE_MAGICCOIN:int = 4;
		
		private var _getCoin:int;
		
		private var _GiveCoin:int;
		public static function getInstance():TreasureBowlMainLogic
		{
			if(_self == null){
				_self = new TreasureBowlMainLogic();
			}
			return _self;
		}
		
		public function TreasureBowlMainLogic()
		{
			_myDis = TreasureBowlMainUI.getInstance();
			_model = ModelLocator.getInstance();
			_bind  = BindManage.getInstance();
		}
		
		/**
		 *初始化 
		 * 
		 */		
		public function Init():void
		{
			
		}
		
		public function beforeShow():void
		{
			addEventListener();
			initUI();
		}
		
		public function afterHide():void
		{
			removeEventListener();
//			if(MultLoader.getInstance().hasSourceByUrl(ResPath.PNG + "jubaopenBG.png")){
//				MultLoader.getInstance().deletSourceByUrl(ResPath.PNG + "jubaopenBG.png");
//			}
			_myDis.icon.source = null;
		}
		
		private function addEventListener():void
		{
			_myDis.windowBG.addEventListener(NxWindowEvent.WINDOW_CLOSE, close);
			
			_myDis.btn1.addEventListener(MouseEvent.CLICK, sendHandler);
			_myDis.btn2.addEventListener(MouseEvent.CLICK, sendHandler);
			_myDis.btn3.addEventListener(MouseEvent.CLICK, sendHandler);
			_myDis.btn4.addEventListener(MouseEvent.CLICK, sendHandler);
			_myDis.shuoming.addEventListener(TextEvent.LINK,TextFun); 
			
			if(_bindFlag == -1){
				_bindFlag = _bind.BindProp(_model.scene.Role, "MagicCoin", this, "magicCoin");
			}
			
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_USE_CORNUCOPIA_SUCCESS), useSuccess);
		}
		
		private function removeEventListener():void
		{
			_myDis.windowBG.removeEventListener(NxWindowEvent.WINDOW_CLOSE, close);
			
			_myDis.btn1.removeEventListener(MouseEvent.CLICK, sendHandler);
			_myDis.btn2.removeEventListener(MouseEvent.CLICK, sendHandler);
			_myDis.btn3.removeEventListener(MouseEvent.CLICK, sendHandler);
			_myDis.btn4.removeEventListener(MouseEvent.CLICK, sendHandler);
			
			_model.removeEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_USE_CORNUCOPIA_SUCCESS), useSuccess);
		}
		private function TextFun(e:TextEvent):void
		{
			switch(e.text)
			{
				case "1600001":
					_model.scene.Role.recharge() ; 
					LogSendLogic.getInstance().btnClickNumSend(22802);
					break ; 
			}
		}
		private function close(e:NxWindowEvent):void
		{
			_myDis.hide();
		}
		
		/**
		 *初始化界面 
		 * 
		 */		
		private function initUI():void
		{
			_myDis.shuoming.htmlText = StaticIni.getSwfLangStr("UIString_1000689");
			var obj:Object = StaticIni.getIniObj("TreasureBowl_" + 1);
			_myDis.label1_1.htmlText = "<font color='#1eff00'>" + obj["Give"] + "</font>" + StaticIni.getSwfLangStr("MagicCoin");
			_myDis.label1_2.htmlText = "<font color='#1eff00'>" + obj["Get"] + "</font>" + StaticIni.getSwfLangStr("MagicCoin")+"<font color='#1eff00'>" +"(" +"+"+obj["Range"]+")"+"</font>";
			obj = StaticIni.getIniObj("TreasureBowl_" + 2);
			_myDis.label2_1.htmlText = "<font color='#1eff00'>" + obj["Give"] + "</font>" + StaticIni.getSwfLangStr("MagicCoin");
			_myDis.label2_2.htmlText = "<font color='#1eff00'>" + obj["Get"] + "</font>" + StaticIni.getSwfLangStr("MagicCoin")+"<font color='#1eff00'>" +"(" +"+"+obj["Range"]+")"+"</font>";
			obj = StaticIni.getIniObj("TreasureBowl_" + 3);
			_myDis.label3_1.htmlText = "<font color='#1eff00'>" + obj["Give"] + "</font>" + StaticIni.getSwfLangStr("MagicCoin");
			_myDis.label3_2.htmlText = "<font color='#1eff00'>" + obj["Get"] + "</font>" + StaticIni.getSwfLangStr("MagicCoin")+"<font color='#1eff00'>" +"(" +"+"+obj["Range"]+")"+"</font>";
			obj = StaticIni.getIniObj("TreasureBowl_" + 4);
			_myDis.label4_1.htmlText = "<font color='#1eff00'>" + obj["Give"] + "</font>" + StaticIni.getSwfLangStr("MagicCoin");
			_myDis.label4_2.htmlText = "<font color='#1eff00'>" + obj["Get"] + "</font>" + StaticIni.getSwfLangStr("MagicCoin")+"<font color='#1eff00'>" +"(" +"+"+obj["Range"]+")"+"</font>";
		}
		
		/**
		 *确认聚宝 
		 * @param e
		 * 
		 */		
		private function sendHandler(e:MouseEvent):void
		{
			var target:DisplayObjectContainer = e.currentTarget as DisplayObjectContainer;
			var obj:Object = new Object();
			switch(target){
				case _myDis.btn1:
					obj = StaticIni.getIniObj("TreasureBowl_" + 1);
					break;
				case _myDis.btn2:
					obj = StaticIni.getIniObj("TreasureBowl_" + 3);
					break;
				case _myDis.btn3:
					obj = StaticIni.getIniObj("TreasureBowl_" + 2);
					break;
				case _myDis.btn4:
					obj = StaticIni.getIniObj("TreasureBowl_" + 4);
					break;
			}
			if(obj["Type"] != TYPE_MAGICCOIN) return;
			
			_getCoin = obj["Get"];
			_GiveCoin = obj["Give"]
			
			if(_model.scene.Role.getMagicCoin()<_GiveCoin)
			{
				AlertUI.showAlert("",StaticIni.getSwfLangStr("UIString_1000702"),AlertUI.OK|AlertUI.CANCEL,MeonyFun);
			}else
			{
				AlertUI.showAlert("",StaticIni.getSwfLangStrVar("UIString_1000697",[_GiveCoin]),AlertUI.OK|AlertUI.CANCEL,YesFun);
			}
//			if(SitAlerLogic.getIntatic()._ui.created ==false)
//			{
//				SitAlerLogic.getIntatic().setUIinfo(StaticIni.getSwfLangStr("UIString_1000697") ,[],YesFun,obj );
//			}
//			
			
		}
		//没钱就充值
		private function MeonyFun(type:int, str:String, clickData:*):void
		{
			if(AlertUI.OK == type )
			{
//				_model.scene.Role.recharge() ; 
//				LogSendLogic.getInstance().btnClickNumSend(22802);
				
			}
		}
		private function YesFun(type:int, str:String, clickData:*):void
		{
			if(AlertUI.OK == type )
			{
				_myDis.btn1.enabled = false;
				_myDis.btn2.enabled = false;
				_myDis.btn3.enabled = false;
				_myDis.btn4.enabled = false;
				
				
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_USE_CORNUCOPIA, int(_GiveCoin));
				
				
				if(_bindFlag != -1){
					_bindFlag = _bind.UnbindProp(_model.scene.Role, "MagicCoin", _bindFlag);
				}
			}
		}
		/**
		 *根据元宝控制按钮 
		 * @param value
		 * 
		 */		
		public function set magicCoin(value:int):void
		{
//			_myDis.btn1.enabled = isEnabled(value, 1);
//			_myDis.btn2.enabled = isEnabled(value, 2);
//			_myDis.btn3.enabled = isEnabled(value, 3);
//			_myDis.btn4.enabled = isEnabled(value, 4);
		}
		
		/**
		 *是否显示 
		 * @param magicCoin
		 * @param index
		 * @return 
		 * 
		 */		
		private function isEnabled(magicCoin:int, index:int):Boolean
		{
			var obj:Object = StaticIni.getIniObj("TreasureBowl_" + index);
			if(obj["Type"] != TYPE_MAGICCOIN) return false;
			var _magicCoin:int = obj["Give"];
			if(magicCoin >= _magicCoin){
				return true;
			} else {
				return false;
			}
		}
		
		/**
		 *兑换成功 
		 * @param e
		 * 
		 */		
		private function useSuccess(e:CustomMsgEvent):void
		{
			var data:Array = e.pList;
			var str:String = StaticIni.getSwfLangStrVar("UIString_1000693", [_getCoin]);
			
			var mc:MovieClip = ResManager.newResClass("jubaopentx") as MovieClip;
			mc.x = 267;
			mc.y = 103;
			_myDis.icon.addChild(mc);
			mc.addFrameScript(mc.totalFrames -1,
				function ():void{
					mc.stop();
					_myDis.icon.removeChild(mc);
					mc = null;
					AlertUI.showAlert(StaticIni.getSwfLangStr("MessageUI_Notice"), str);
				});
		}
	}
}